<?php
session_start ();
require_once ("../../commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');

$_SESSION ['pathway'] = '../..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include_once ($_SESSION ['pathway'] . "/header.php");

// ~ print_r($_POST);
// ~ echo "</br>";
// ~ print_r($_FILES);
// ~ print_r($_SESSION);

if (isset ( $_GET ['action'] )) {
	$action = $_GET ['action'];
}
if (isset ( $_GET ['index'] )) {
	$index = $_GET ['index'];
}
foreach ( $_POST as $key => $value ) {
	if ($key != '') {
		$value = mysql_real_escape_string ( $value );
		// ~ $value = mysqli_real_escape_string($link,$value);
		$value = str_replace ( ',', '.', $value );
		$_POST [$key] = $value;
	}
}

$allfichiers = '';
if (isset ( $_POST ['code1'] )) {
	$code1 = strtoupper ( $_POST ['code1'] );
} else {
	$code1 = '';
}
if (isset ( $_POST ['code2'] )) {
	$code2 = strtoupper ( $_POST ['code2'] );
} else {
	$code2 = '';
}
if (isset ( $_POST ['code3'] )) {
	$code3 = strtoupper ( $_POST ['code3'] );
} else {
	$code3 = '';
}
if (isset ( $_POST ['code4'] )) {
	$code4 = strtoupper ( $_POST ['code4'] );
} else {
	$code4 = '';
}
if (isset ( $_POST ['code5'] )) {
	$code5 = strtoupper ( $_POST ['code5'] );
} else {
	$code5 = '';
}
if (isset ( $_POST ['codeexterne'] )) {
	$codeexterne = $_POST ['codeexterne'];
} else {
	$codeexterne = '';
}
if (isset ( $_POST ['codegenethon'] )) {
	$codegenethon = $_POST ['codegenethon'];
} else {
	$codegenethon = '';
}
if (isset ( $_POST ['centre'] ) and $_POST ['centre'] != '') {
	$centre = $_POST ['centre'];
	strtoupper ( $centre );
} else {
	$centre = '';
}
if (isset ( $_POST ['neurologist'] ) and $_POST ['neurologist'] != '') {
	$neurologist = $_POST ['neurologist'];
	$neurologist = strtoupper ( $neurologist );
} else {
	$neurologist = '';
}
if (isset ( $_POST ['date'] ) and $_POST ['date'] != '') {
	$date = $_POST ['date'];
} else {
	$date = '';
}
if (isset ( $_POST ['statut'] ) and $_POST ['statut'] != '') {
	$statut = $_POST ['statut'];
} else {
	$statut = '';
}
if (isset ( $_POST ['user'] ) and $_POST ['user'] != '') {
	$user = strtoupper ( $_POST ['user'] );
} else {
	$user = '';
}
$last_modifier = strtoupper ( $_SESSION ['id'] ); // dès qu'on soumet le formulaire, le last_modifier est actualisé;}
if (isset ( $_POST ['alias'] ) and $_POST ['alias'] != '') {
	$alias = $_POST ['alias'];
} else {
	$alias = '';
}
if (isset ( $_POST ['gene'] ) and $_POST ['gene'] != '') {
	$gene = $_POST ['gene'];
} else {
	$gene = '';
}
if (isset ( $_POST ['haplotype'] ) and $_POST ['haplotype'] != '') {
	$haplotype = $_POST ['haplotype'];
} else {
	$haplotype = '';
} // cas ou l'on vient du modif_index
if (isset ( $_POST ['linkage'] ) and $_POST ['linkage'] != '') {
	$linkage = $_POST ['linkage'];
} else {
	$linkage = '';
}
if (isset ( $_POST ['lodscore'] ) and $_POST ['lodscore'] != '') {
	$lodscore = $_POST ['lodscore'];
} else {
	$lodscore = '';
}
if (isset ( $_POST ['commentairelinkage'] ) and $_POST ['commentairelinkage'] != '') {
	$commentairelinkage = $_POST ['commentairelinkage'];
	$commentairelinkage = mysql_real_escape_string ( $commentairelinkage );
} else {
	$commentairelinkage = '';
}
// ~ if(isset($_POST['commentairelinkage']) and $_POST['commentairelinkage'] != ''){ $commentairelinkage = $_POST['commentairelinkage']; $commentairelinkage = mysqli_real_escape_string($link,$commentairelinkage);}else{ $commentairelinkage ='';}
if (isset ( $_POST ['modelinkage'] ) and $_POST ['modelinkage'] != '') {
	$modelinkage = $_POST ['modelinkage'];
} else {
	$modelinkage = '';
}
if (isset ( $_POST ['diagnosticlinkage'] ) and $_POST ['diagnosticlinkage'] != '') {
	$diagnosticlinkage = $_POST ['diagnosticlinkage'];
} else {
	$diagnosticlinkage = '';
}
if (isset ( $_POST ['validation'] ) and $_POST ['validation'] != '') {
	$validation = $_POST ['validation'];
} else {
	$validation = '';
}
if (isset ( $_POST ['afaire'] ) and $_POST ['afaire'] != '') {
	$afaire = $_POST ['afaire'];
	$afaire = mysql_real_escape_string ( $afaire );
} else {
	$afaire = '';
}
// ~ if(isset($_POST['afaire']) and $_POST['afaire'] != ''){ $afaire = $_POST['afaire']; $afaire = mysqli_real_escape_string($link,$afaire);}else{ $afaire ='';}
if (isset ( $_POST ['commentairesconclusion'] )) {
	$commentairesconclusion = $_POST ['commentairesconclusion'];
	$commentairesconclusion = mysql_real_escape_string ( $commentairesconclusion );
} else {
	$commentairesconclusion = '';
}
// ~ if(isset($_POST['commentairesconclusion'])){ $commentairesconclusion = $_POST['commentairesconclusion']; $commentairesconclusion = mysqli_real_escape_string($link,$commentairesconclusion);}else{ $commentairesconclusion ='';}
if (isset ( $_POST ['fichiersattaches'] ) and $_POST ['fichiersattaches'] != '') {
	$fichiersattaches = $_POST ['fichiersattaches'];
} else {
	$fichiersattaches = '';
}

// ~ echo $last_modifier;

if (isset ( $_FILES ['fichieraajouter'] ) and $_FILES ['fichieraajouter'] ['error'] == 0) {
	$tabledate = getdate ();
	$id_unique = $tabledate ['seconds'] . $tabledate ['minutes'] . $tabledate ['hours'] . $tabledate ['mday'] . $tabledate ['mon'] . $tabledate ['year'];
	
	move_uploaded_file ( $_FILES ['fichieraajouter'] ['tmp_name'], '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) );
	chmod ( '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ), 0777 );
	$allfichiers .= $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) . ";";
}

$allfichiers .= $fichiersattaches;

// ~ echo "fichieraajouter ".$_FILES['fichieraajouter']['name']."</br>";
// ~ echo "fichiersattaches $fichiersattaches</br>";
// ~ echo "allfichiers $allfichiers</br>";

if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'edit') {
	$update = "UPDATE `$db`.`linkage` SET 
	`code1` = '$code1',
	`code2` = '$code2',
	`code3` = '$code3',
	`code4` = '$code4',
	`codeexterne` = '$codeexterne',
	`codegenethon` = '$codegenethon',
	`centre` = '$centre',
	`neurologist` = '$neurologist',
	`modelinkage` = '$modelinkage',
	`alias` = '$alias',
	`gene` = '$gene',
	`haplotype` = '$haplotype',
	`linkage` = '$linkage',
	`lodscore` = '$lodscore',
	`commentairelinkage` = '$commentairelinkage',
	`date` = '$date',
	`statut` = '$statut',
	`user` = '$user',
	`last_modifier` = '$last_modifier',
	`fichiersattaches` = '$allfichiers',
	`diagnosticlinkage` = '$diagnosticlinkage',
	`afaire` = '$afaire',
	`validation` = '$validation',
	`commentairesconclusion` = '$commentairesconclusion'
	WHERE `linkage`.`index` = '$index';";
	// ~ echo $update;
	mysql_query ( mysql_real_escape_string($update) ) or die ( "Can't modify entry : " . mysql_error () );
	// ~ mysqli_query($link,$update) or die("Can't modify entry : " . mysql_error());
} elseif (isset ( $_GET ['action'] ) and $_GET ['action'] == 'insert') {
	$recupereindexmaxlinkage = "select MAX(`index`) FROM `linkage`";
	$resultindex = mysql_query ( $recupereindexmaxlinkage ) or die ( "Impossible de r&eacute;cup&eacute;rer l'index de la table linkage" );
	// ~ $resultindex = mysqli_query($link,$recupereindexmaxlinkage) or die ("Impossible de r&eacute;cup&eacute;rer l'index de la table linkage");
	while ( $ligne = mysql_fetch_row ( $resultindex ) ) {
		// ~ while ($ligne = mysqli_fetch_row($resultindex)) {
		$indexlinkage = $ligne [0] + 1;
	}
	$insertlinkage = "INSERT INTO `$db`.`linkage` (`index`, `code1`, `code2`, `code3`, `code4`, `codeexterne`, `codegenethon`,`centre`, `neurologist`, `modelinkage`, `alias`, `haplotype`,`linkage`, `lodscore`, `commentairelinkage`, `date`, `statut`, `user`, `last_modifier`,`fichiersattaches`, `diagnosticlinkage`, `afaire`, `validation`, `commentairesconclusion`) 
	VALUES ('$indexlinkage','$code1', '$code2','$code3','$code4','$codeexterne', '$codegenethon','$centre', '$neurologist', '$modelinkage', '$alias', '$haplotype','$linkage', '$lodscore', '$commentairelinkage', '$date', '$statut', '$user', '$last_modifier','$allfichiers', '$diagnosticlinkage', '$afaire', '$validation', '$commentairesconclusion');";
	mysql_query ( mysql_real_escape_string($insertlinkage) ) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table linkage : " . mysql_error () );
	// ~ mysqli_query($link,$insertlinkage) or die("Impossible d'ins&eacute;rer donn&eacute;es dans table linkage : " . mysql_error());
	// ~ echo $insertlinkage;
}

if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'upload') {
	$_POST = array ();
	echo "<script>window.location.href=\"../linkage.php?action=edit&index=" . $index . "&table=linkage\"</script>";
} else if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'Ok') {
	$_POST = array ();
	echo "<script>window.location.href=\"./../../start.php\"</script>";
} else {
	$_POST = array ();
	echo "<script>window.location.href=\"../linkage.php?action=edit&index=" . $index . "&table=linkage\"</script>";
}

?>
